package com.relife.core.easyui;

import com.relife.common.model.Client;
import com.relife.common.model.ClientManager;
import com.relife.common.utils.StringUtil;
import com.relife.core.entity.FunctionEntity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;


public class MenuTag extends TagSupport {

	private static final long serialVersionUID = -4134622892594943541L;
	private static final String TOP_MENU = "top";
	private static final String LEFT_MENU = "left";
	private String type;

	@Override
	public int doStartTag() throws JspTagException {
		return EVAL_BODY_INCLUDE;
	}

	@Override
	public int doEndTag() throws JspException {
		try {
			JspWriter out = this.pageContext.getOut();
			if (TOP_MENU.equals(type)) {
				out.print(this.gettopMenu());

			} else if (LEFT_MENU.equals(type)) {
				out.print(this.getLeftMenu());

			}
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	/**
	 * 
	 * @author WU
	 * @date 2016年5月27日 下午4:27:48
	 * @return
	 */
	private StringBuffer gettopMenu() {
		StringBuffer sb = new StringBuffer("");
		Client client = ClientManager.getInstance().getClientSession();

		sb.append("<ul class=\"left\">\n");
		sb.append("<li class=\"line\"></li>\n");
		for (FunctionEntity fun : client.getfList()) {
			if (fun.getParent() == null) {
				sb.append("<li id=\"" + fun.getId() + "\">" + fun.getName() + "</li><li class=\"line\"></li>\n");
			}
		}
		sb.append("</ul>\n");
		return sb;
	}

	/**
	 * 
	 * @author WU
	 * @date 2016年5月27日 下午4:27:24
	 * @return
	 */
	private StringBuffer getLeftMenu() {
		StringBuffer sb = new StringBuffer("");
		Client client = ClientManager.getInstance().getClientSession();
		List<FunctionEntity> topList = new ArrayList<FunctionEntity>();
		Map<String, List<FunctionEntity>> map = new HashMap<String, List<FunctionEntity>>();
		String path = this.pageContext.getRequest().getServletContext().getContextPath() + "/";

		for (FunctionEntity fun : client.getfList()) {
			if (fun.getParent() != null) {
				if (map.get(fun.getParent().getId()) != null) {
					map.get(fun.getParent().getId()).add(fun);
				} else {
					List<FunctionEntity> fList = new ArrayList<FunctionEntity>();
					fList.add(fun);
					map.put(fun.getParent().getId(), fList);
				}
			} else {
				topList.add(fun);
			}
		}

		for (FunctionEntity fun : topList) {
			sb.append("<ul pId=\"" + fun.getId() + "\" class=\"easyui-tree\" data-options=\"animate:true,onClick:onClickMenu\" style=\"display:none\">\n");
			if (map.get(fun.getId()) != null) {
				for (FunctionEntity f : map.get(fun.getId())) {
					if (StringUtil.isEmpty(f.getUrl())) {
						sb.append("<li id=\"" + f.getId() + "\" state=\"closed\"><span>" + f.getName() + "</span>\n");

					} else {
						sb.append("<li id=\"" + f.getId() + "\" iconCls=\"" + path + f.getUrl() + "\" state=\"open\"><span>" + f.getName() + "</span>\n");

					}
					if (map.get(f.getId()) != null) {
						sb.append("<ul>\n");
						for (FunctionEntity fu : map.get(f.getId())) {
							sb.append("<li id=\"" + fu.getId() + "\" iconCls=\"" + path + fu.getUrl() + "\" state=\"open\"><span>" + fu.getName() + "</span></li>\n");
						}
						sb.append("</ul>\n");
					}
				}
			}
			sb.append("</li>\n");
			sb.append("</ul>\n");
		}
		return sb;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
}
